"""
路由
"""

from fastapi import FastAPI
from fastapi._compat import ModelField  # noqa: ignore[protected member]
from fastapi.routing import APIRoute
from pydantic import BaseModel


def get_route_path_2_model(app: FastAPI) -> dict[str, type[BaseModel]]:
    """
    获取 路由-模型 映射

    Args:
        app: 项目 App

    Returns:
        路由-模型 映射
    """

    route_path_2_model: dict[str, type[BaseModel]] = {}

    for route in app.routes:
        if (
            isinstance(route, APIRoute)
            and route.body_field
        ):
            body_field: ModelField = route.body_field
            route_path_2_model[route.path] = body_field.type_

    return route_path_2_model


def update_route(app: FastAPI) -> None:
    """ 更新接口路由 """

    from biz.api_route_biz import APIRouteSetter

    APIRouteSetter.update_api_route(app)
